package mysgoog.pattern.influence;

import java.io.*;
import java.util.*;

import mysgoog.pattern.Storage.ValuePair;
/**
* @author Fu, Zhongyang
*/
public class InfluencePatternLoader
{

	static int c = 0;
    private static Map<Integer, Map<String, ValuePair>> mapList = null;  

    public static void load(Map<Integer, Map<String, ValuePair>> map, String patternLocation) throws FileNotFoundException, IOException, ClassNotFoundException
    {   	
    	mapList = map;
        //read("pattern\\influence.dbpattern");
    	read(patternLocation);
        //readFromObject(map);
    }
    
    public static void readFromObject(Map<Integer, Map<String, ValuePair>> map) throws FileNotFoundException, IOException, ClassNotFoundException{
        ObjectInputStream in =
       	   new ObjectInputStream( 
       	     new FileInputStream("pattern\\influence.dat"));
       	  map = (Map<Integer, Map<String, ValuePair>>) in.readObject();
       	  in.close();
    }
    
    public static void generate() throws FileNotFoundException, IOException{
    	mapList = new HashMap<Integer, Map<String, ValuePair>>();
    	read("pattern\\influence.dbpattern");
        ObjectOutputStream out =
      	   new ObjectOutputStream( 
      	     new FileOutputStream("pattern\\influence.dat"));
      	  out.writeObject(mapList);
      	  out.flush();
      	  out.close();
    }
    
    public static void main(String[] arg) throws FileNotFoundException, IOException{
    	generate();
    }

    public static void read(String filename) throws FileNotFoundException, IOException
    {
        BufferedReader reader = null;
        try
        {
            File file = new File(filename);
            reader = new BufferedReader(new FileReader(file));
            String text = null;
            while ((text = reader.readLine()) != null)
            {
                parse(text);
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if (reader != null)
                {
                    reader.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    private static void parse(String pattern)
    {
        String[] token = pattern.split(" ");
        int num = Integer.parseInt(token[0]) * 13 + Integer.parseInt(token[1]);

        ValuePair value = new ValuePair(Integer.parseInt(token[2]), Integer.parseInt(token[3]), Integer.parseInt(token[4]));
        generatePattern(num, token[5], value);
    }
    
    private static void generatePattern(int num, String pattern, ValuePair value){
		generatePatterno(num, pattern, value);
    }
    
    private static void generatePatterno(int num, String pattern, ValuePair value){
		String newPattern = pattern.replaceFirst("o", "O");
		if (!pattern.equals(newPattern)){
			generatePatterno(num, newPattern, value);
			newPattern = pattern.replaceFirst("o", ".");
			generatePatterno(num, newPattern, value);
		} else		
		generatePatternx(num, pattern, value);
    }
    private static void generatePatternx(int num, String pattern, ValuePair value){
		String newPattern = pattern.replaceFirst("x", "X");
		if (!pattern.equals(newPattern)){
			generatePattern(num, newPattern, value);
			newPattern = pattern.replaceFirst("x", ".");
			generatePattern(num, newPattern, value);
		} else
		generatePatternq(num, pattern, value);
    }
    private static void generatePatternq(int num, String pattern, ValuePair value){
		String newPattern = pattern.replaceFirst("[?]", ".");
		if (!pattern.equals(newPattern)){
			generatePattern(num, newPattern, value);
			newPattern = pattern.replaceFirst("[?]", "X");
			generatePattern(num, newPattern, value);
			newPattern = pattern.replaceFirst("[?]", "O");
			generatePattern(num, newPattern, value);
		} else 
		{
			//System.out.println(c++ + " " + pattern);
			putInMap(num, pattern, value);   		
		}   		
    }
    private static void putInMap(int num, String pattern, ValuePair value){
        Map<String, ValuePair> map;
        if (mapList.containsKey(num)) map = mapList.get(num);
        else{
        	map = new HashMap<String, ValuePair>();
        	mapList.put(num, map);
        }
    	map.put(pattern, value);
    	
    }
    
}
